/**
 * Created with IntelliJ IDEA
 * Description:
 * User:恋恋
 * Date:2022-11-23
 * Time:18:46
 */
class MyRunnable implements Runnable {
    @Override
    public void run() {
        while (true) {
            try {
                System.out.println("hello runnable");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

    class MyThread extends Thread {


        @Override
        public void run() {
            while (true) {
                try {
                    System.out.println("hello Thread");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
    }

    public class ThreadDemo {
        public static void main(String[] args) {
//            //继承Thread类,实现子类，重写run
//            Thread thread = new MyThread();
//            //thread.run();
//            thread.start();
//            while (true) {
//                try {
//                    System.out.println("hello main");
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
            //继承Runnable类,实现子类，重写run
//            Runnable runnable = new MyRunnable();
//
//            Thread thread1 = new Thread(runnable);
//
//            thread1.start();
//
//            while (true) {
//                try {
//                    System.out.println("hello main");
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }

//使用匿名内部类，继承Thread
        Thread thread2 = new MyThread(){
            @java.lang.Override
            public void run() {
                while (true){
                    System.out.println("hello 匿名内部Thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        };
              thread2.start();
            while (true) {
                try {
                    System.out.println("hello main");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

//使用匿名内部类，继承Runnable
            //切记这里不能使用自己继承的子类来替换Thread, 因为我们自己继承的子类，是没有重写构造方法的，因此传递的Runnable对象他是不认识的。
//            Thread thread3 = new Thread(new Runnable() {
//                @java.lang.Override
//                public void run() {
//                    while (true) {
//                        try {
//                            System.out.println("hello 匿名内部Runnable");
//                            Thread.sleep(1000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                }
//            });
//            thread3.start();
//            while (true) {
//                try {
//                    System.out.println("hello main");
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//
//            }
            //lambda表达式
//            Thread thread5 = new Thread(()->{
//                System.out.println("Lambda表达式");
//            });
//            thread5.start();;
//            System.out.println("main");
        }
    }
